package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"pushserver"
	"pushserver/persistence"
	"strings"
)

type testFilter struct {
}

func (this testFilter) Filter(conn net.Conn) bool {
	fmt.Println(conn.RemoteAddr().String())
	return true
}

func main() {
	sender, listener, err := pushserver.NewDefaultServer(false)
	if err != nil {
		fmt.Println(err)
	}

	go func() {
		listener.Listen("127.0.0.1:3366")
	}()
	go func() {
		apier := pushserver.Api{
			Sender: &sender.Sender,
		}
		// 开启http api
		apier.Listen("127.0.0.1:3365", "/msg/sender")
	}()
	// 消息发送重试次数
	sender.ResendTimes = 2
	// 重试间隔（毫秒）
	sender.ResendInterval = 10 * 1000
	// 设置离线消息容器
	sender.Container = persistence.CreateMsgContainer("default")
	//sdr.Container=persistence.CreateMsgContainer("redis",
	//	&persistence.RedisOptions{Network:"tcp",Address:"127.0.0.1:6379",})
	// 消息发送处理最大并行数设置
	sender.MaxParallel = 10
	//最小并行数设置
	sender.MinParallel = 1
	//队列中消息堆积阈值设置
	sender.QueueBufferLen = 1000
	// 开始sender
	sender.BeginSender()
	for {
		// 从标准输入读取字符串，以\n为分割
		fmt.Print("input a msg\n")
		text, err := bufio.NewReader(os.Stdin).ReadString('\n')
		if err == nil {
			text = strings.Replace(text, "\r\n", "", 1)
			if text == "count" {
				fmt.Println(listener.OnlineCount())
			} else {
				sender.SendNotice(text)
			}
		}
	}
}
